//
//  LCFileManager.m
//  油递员(车队端)
//
//  Created by puman on 16/3/31.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMFileManager.h"

@implementation RMMFileManager

+ (RMMFileManager *)shareHandle{
    
    static RMMFileManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager                        = [[RMMFileManager alloc] init];
            manager.userDefaults           = [NSUserDefaults standardUserDefaults];
            manager.bookingDic             = [NSMutableDictionary dictionary];
            manager.skillBookingArray      = [NSMutableArray array];
            manager.loginType              = nil;
            manager.openid                 = nil;
            manager.isLoginSuccess         = NO;
            manager.isAddBookingOrder      = NO;
        }
    });
    
    return manager;
}
// 加密的方法
+ (NSString *)getMD5WithString:(NSString *)time{
    NSString *string = [NSString stringWithFormat:@"appid=%@&secretkey=%@&timestamp=%@",APPID,PUBLICKKEY,time];
    const char *cStr = [string UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest );
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02x", digest[i]];
    return result;
}
// 得到时间戳的方法
+ (NSString *)getTimesTamp{
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970] * 1000;
    double date         = (double)time;
    NSString *timeStr   = [NSString stringWithFormat:@"%.0f",date];
    return timeStr;
}
// 判断网络状态的方法
+ (BOOL)isNetwork{
    Reachability *reachability = [Reachability reachabilityWithHostname:@"www.apple.com"];
    switch ([reachability currentReachabilityStatus]) {
        case NotReachable:
            return YES;
            break;
        case ReachableViaWWAN:
            return NO;
            break;
        case ReachableViaWiFi:
            return NO;
            break;
            
        default:
            break;
    }
}

+ (BOOL)isPhoneNumberWith:(NSString *)phoneNumber{
    NSString *number = @"^1[3|4|5|7|8][0-9]\\d{8}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    if ([numberPre evaluateWithObject:phoneNumber]) {
        return YES;
    }
    return NO;
}

- (void)saveVale:(id)value forKey:(NSString *)key{
    [self.userDefaults setObject:value forKey:key];
    [self.userDefaults synchronize];
}
+ (void)removeObjForKey:(NSString *)key{
    [[RMMFileManager shareHandle].userDefaults removeObjectForKey:key];
}

- (id)getValeForKey:(NSString *)key{
    id value = [self.userDefaults objectForKey:key];
    return value;
}
+ (void)MBProgressWith:(NSString *)msg onView:(UIView *)view;{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.labelText = msg;
    // hud.labelColor =kBasicColor;
   // hud.color = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [hud hide:YES afterDelay:1.5];
}
// 改变label上字体的颜色
+ (void)changeLabelTextColorWithLabel:(UILabel *)label secondeString:(NSString *)string forRange:(NSRange)range byColor:(UIColor *)color{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
    
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(range.length + 8, string.length + 1)];
    [label setAttributedText:attributedString];
}
+ (void)changeLabelTextColorOneWithLabel:(UILabel *)label forRange:(NSRange)range byColor:(UIColor *)color{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text];
    [attributedString addAttribute:NSForegroundColorAttributeName value:color range:range];
    [label setAttributedText:attributedString];
}
+ (BOOL)OnlyAllowEnterNumbersAndDecimalWith:(NSString *)string{
    NSString *number = @"^[0-9]+([.]{0,1}[0-9]+){0,1}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    if ([numberPre evaluateWithObject:string]) {
        return YES;
    } else {
        return NO;
    }
}
+ (BOOL)onelyAllowEnterChinese:(NSString *)string{
    NSString *number = @"^[\u4e00-\u9fa5]{0,}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    if ([numberPre evaluateWithObject:string]) {
        return YES;
    } else {
        return NO;
    }
}
+ (BOOL)onelyAllowEnterEnglish:(NSString *)string{
    NSString *number = @"^[A-Za-z]+$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    if ([numberPre evaluateWithObject:string]) {
        return YES;
    } else {
        return NO;
    }
 }
+ (BOOL)validationIdCardWith:(NSString *)string{
    NSString *number = @"\\d{14}[[0-9],0-9xX]";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    if ([numberPre evaluateWithObject:string]) {
        return YES;
    } else {
        return NO;
    }
}
+ (NSString *)getNowTimeWithFormatterString:(NSString *)formatterString{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatterString];
    NSString *nowTime = [formatter stringFromDate:date];
    return nowTime;
}
+ (void)callKeFuWithPhoneNumber:(NSString *)number onView:(UIView *)view{
    NSString *phoneNumber = [number stringByReplacingOccurrencesOfString:@"-" withString:@""];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]];
    UIWebView *callView = [[UIWebView alloc] init];
    [callView loadRequest:[NSURLRequest requestWithURL:url]];
    [view addSubview:callView];
}
+ (void)openUserInteractionEnabledWith:(UIButton *)button{
    [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
    button.backgroundColor = kBasicColor;
}
+ (void)offUserInteractionEnabled:(UIButton *)button{
    button.backgroundColor = RGBA(237, 238, 239, 1);
    [button setTitleColor:RGBA(212, 213, 214, 1) forState:(UIControlStateNormal)];
}
+ (NSString *)formatStringFor:(NSDictionary *)dic{
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *key in dic) {
        id value = dic[key];
        NSString *string = [NSString stringWithFormat:@"%@=%@",key,value];
        [array addObject:string];
    }
    return [array componentsJoinedByString:@"&"];
}
+ (UIButton *)addInputViewForTextField{
    UIButton *button =  [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(0, 0, kScreenWideth, 40);
    [button setTitle:@"完成" forState:(UIControlStateNormal)];
    [button setTitleColor:kBasicColor forState:(UIControlStateNormal)];
    button.backgroundColor = kWhiteColor;
    button.layer.borderColor = RGBA(198, 197, 198, 1).CGColor;
    button.layer.borderWidth = 1;
    return button;
}
+ (id)retrunObjWith:(id)obj{
    if ([obj isKindOfClass:[NSNull class]]) {
        return @"";
    }
    NSString *string = [NSString stringWithFormat:@"%@",obj];
    if ([string isEqualToString:@"(null)"]) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",obj];
}
+ (void)butOnWithBut:(UIButton *)button{
    button.userInteractionEnabled = YES;
    button.backgroundColor = kBasicColor;
    [button setTitleColor:kWhiteColor forState:(UIControlStateNormal)];
}
+ (void)butOffWithBut:(UIButton *)button{
    button.userInteractionEnabled = NO;
    button.backgroundColor = RGBA(237, 238, 239, 1);
    [button setTitleColor:RGBA(212, 213, 214, 1) forState:(UIControlStateNormal)];
}
+ (NSDictionary *)setValueForDic:(NSDictionary *)dicrionary{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dicrionary];
    NSArray *array = [dic allKeys];
    for (int i = 0; i < array.count; i++) {
        NSString *key = array[i];
        id value = dic[key];
        if ([value isKindOfClass:[NSNull class]]) {
            value = @"";
            [dic setObject:value forKey:key];
        }
    }
    return dic;
//    if ([dicrionary isKindOfClass:[NSDictionary class]]) {
//        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dicrionary];
//        NSArray *array = [dic allKeys];
//        for (int i = 0; i < array.count; i++) {
//            NSString *key = array[i];
//            id value = dic[key];
//            if ([value isKindOfClass:[NSNull class]]) {
//                value = @"";
//                [dic setObject:value forKey:key];
//            }
//        }
//        return dic;
//    } else {
//        return nil;
 //   }
}

@end
